/**
 * Copyright (c) 2024,  科泰集团教研团队
 * 版权所有，请勿侵权
 */
package com.ktjiaoyu.utils.fastjson2;

import com.alibaba.fastjson2.JSONWriter;
import com.alibaba.fastjson2.filter.PropertyPreFilter;

import java.util.Set;

/**
 * @author 科泰集团教研团队
 * @version 1.0.0
 * @packageName com.ktjiaoyu.utils.fastjson2
 * @fileName FastJson2SensitivePropFilter.java
 * @createTime 2024/10/20 14:29
 * @lastModify 2024/10/20 14:29
 */
public class FastJson2SensitivePropFilter implements PropertyPreFilter {
    /** 通过构造器注入需要排除的属性集合 */
    public FastJson2SensitivePropFilter(Set<String> excludes) {
        this.excludes = excludes;
    }
    private Set<String> excludes;
    /** 处理字段，返回true表示字段需要序列化，返回false表示不需要 **/
    @Override
    public boolean process(JSONWriter jsonWriter,
                           Object obj, String propertyName) {
        if (excludes == null) {
            // 没有排除的属性，直接允许
            return true;
        }
        // 不包含在excludes中的属性才需要序列化
        boolean isNeed = !excludes.contains(propertyName);
        return isNeed;
    }
}
